<?php
/**
 * (jobs) 备货，站点在途调整
 */
namespace App\Jobs\Stocking;

use App\Models\Inventory\AdjustAreaTransit;
use App\Models\Inventory\AreaCn;
use App\Models\SsBasic\AreaSkuStock;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SetAreaTransit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $areaId;
    protected $pSkuId;
    protected $oldTransit;
    protected $newTransit;
    protected $userId;

    /**
     * SetAreaTransit constructor.
     * @param $areaId
     * @param $pSkuId
     * @param $oldTransit
     * @param $newTransit
     * @param $userId
     */
    public function __construct($areaId,$pSkuId,$oldTransit,$newTransit,$userId)
    {
        $this->areaId = $areaId;
        $this->pSkuId = $pSkuId;
        $this->oldTransit = $oldTransit;
        $this->newTransit = $newTransit;
        $this->userId = $userId;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->areaId,$this->pSkuId,$this->oldTransit,$this->newTransit,$this->userId);
    }

    private function exeFillData($areaId,$pSkuId,$oldTransit,$newTransit,$userId){
        AreaCn::query()->updateOrCreate(
            ['area_id' => $areaId,'psku_id' => $pSkuId],
            ['area_transit' => $newTransit]
        );
        AreaSkuStock::query()->updateOrCreate(['area_psku' => $areaId.'_'.$pSkuId],['cn_transit_num' => $newTransit]);
        AdjustAreaTransit::query()->create(
            [
                'area_id' => $areaId,
                'psku_id' => $pSkuId,
                'b_num' => $oldTransit,
                'a_num' => $newTransit,
                'auser_id' => $userId,
            ]
        );
    }
}
